package cn.xny.system.controller;


import cn.xny.basic.util.AjaxResult;
import cn.xny.basic.util.PageList;
import cn.xny.system.domain.Dictionarytype;
import cn.xny.system.query.DictionarytypeQuery;
import cn.xny.system.service.DictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：数据字典
 */
@Api(value = "数据字典接口类", description = "处理数据字典的CRUD操作")
@RestController
@RequestMapping("/dictionarytype")
public class DictionarytypeController {


    @Autowired
    private DictionarytypeService dictionarytypeService;

    /**
     * 查询单个
     */

    @ApiOperation(value = "查询单个")
    @GetMapping("/{id}")
    public Dictionarytype findOne(@PathVariable("id") Long id) {


        return dictionarytypeService.loadByAll(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @ApiOperation(value = "查询全部")
    @GetMapping
    public List findAll() {

        return dictionarytypeService.loadAll();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {


            dictionarytypeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");

        }

    }

    /**
     * 添加或者修改
     */
    @ApiOperation(value = "添加或者修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype) {
        try {
            if (dictionarytype.getId() == null) {
                dictionarytypeService.save(dictionarytype);
            } else {

                dictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();

            return new AjaxResult(false, "操作失败");
        }

    }

    /**
     * 分页加高级查询
     *
     * @param dictionarytypeQuery
     * @return
     */
    @ApiOperation(value = " 分页加高级查询")
    @PostMapping
    public PageList<Dictionarytype> pageQuery(@RequestBody DictionarytypeQuery dictionarytypeQuery) {

        return dictionarytypeService.pageQuery(dictionarytypeQuery);

    }

    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            dictionarytypeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");

        }

    }


}
